package hl.demosample.ui.fragment.home

import android.os.Bundle
import android.text.TextUtils
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.ToastUtils
import hl.demosample.R
import hl.demosample.app.appViewModel
import hl.demosample.app.base.BaseFragment
import hl.demosample.databinding.FragmentHomeBinding
import hl.demosample.viewmodel.state.HomeViewModel
import kotlinx.android.synthetic.main.include_toolbar.*
import hl.demosample.app.ext.init
import hl.demosample.app.ext.initFloatBtn
import hl.demosample.app.ext.setAdapterAnimation
import hl.demosample.app.ext.setUiTheme
import hl.demosample.app.util.ListDataUtil
import hl.demosample.app.weight.recycleview.SpaceItemDecoration
import hl.demosample.data.model.bean.HomeData
import hl.demosample.ui.adapter.HomeAdapter
import kotlinx.android.synthetic.main.fragment_home.*
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

/*
* path: DemoSample 
* Author: 86133
* Create: 2021-06-01 16:10
* Changes (from 2021-06-01)
* Desc:
*
*/
class HomeFragment : BaseFragment<HomeViewModel, FragmentHomeBinding>() {

    //适配器
    private val articleAdapter: HomeAdapter by lazy { HomeAdapter(arrayListOf(), true) }

    override fun layoutId() = R.layout.fragment_home

    override fun initView(savedInstanceState: Bundle?) {
        toolbar.run {
            init("Demo首页")
            inflateMenu(R.menu.home_menu)
            setOnMenuItemClickListener {
                when (it.itemId) {
                    R.id.home_search -> {
                    }
                }
                true
            }
        }
        //初始化recyclerView
        recyclerView.init(LinearLayoutManager(context),articleAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0,ConvertUtils.dp2px(8f), true))
            it.initFloatBtn(floatbtn,false)
        }
        articleAdapter.data = ListDataUtil.HOME_DATA

        articleAdapter.run {
            setCollectClick { item, v, position ->
                if (v.isChecked) {

                } else {

                }
            }

            setOnItemClickListener { adapter, view, position ->
                 val homeData = adapter.getItem(position) as (HomeData)
                when{
                    TextUtils.equals("login",homeData.type) -> {
                     nav().navigateAction(R.id.action_to_loginFragment)
                    }
                    TextUtils.equals("pop_window",homeData.type) -> {
                        nav().navigateAction(R.id.action_mainfragment_to_popWindowFragment)
                    }
                    TextUtils.equals("progress",homeData.type) -> {
                        nav().navigateAction(R.id.action_mainfragment_to_circleProgressFragment)
                    }
                }
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
       appViewModel.run {
           //监听全局的主题颜色改变
           appColor.observeInFragment(this@HomeFragment) {
               setUiTheme(it, toolbar)
           }
           //监听全局的列表动画改编
           appAnimation.observeInFragment(this@HomeFragment) {
               articleAdapter.setAdapterAnimation(it)
           }
       }
    }
}